package com.yehaikeji.workflow.controller.management;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yehaikeji.workflow.bean.user;
import com.yehaikeji.workflow.common.assist.assist;
import com.yehaikeji.workflow.controller.bean.ResultMessage;
import com.yehaikeji.workflow.controller.bean.resultlist;
import com.yehaikeji.workflow.service.userService;
import com.yehaikeji.workflow.util.Verification;

import io.swagger.annotations.ApiOperation;

/**
 * @author ybt
 *
 */
@Controller
@RequestMapping("userManager")
public class userController {
	@Autowired
	private userService userService;

	@RequestMapping(value = "/login")
	private String managerlogin() {
		return "login";
	}
	
	@RequestMapping(value = "/manage")
	private String manageradmin(ModelMap map) {
		map.put("ismenu",3);
		return "user";
	}
	
	@RequestMapping(value = "/exit")
	private String manageradmin(HttpSession session) {
		session.invalidate();
		return "login";
	}
	
	@RequestMapping(value = "/signature")
	private String signature(String id,ModelMap map) {
		map.put("incidentid", id);
		return "signature";
	}

	@RequestMapping(value = "/insertUser", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "新增用户", notes = "添加一个用户到数据库")
	private ResultMessage insertUser(String name,String password,Integer type) throws UnsupportedEncodingException {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(name) ||type == null
					|| StringUtil.isEmpty(password)) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数不能为空");
			} else {
				String pattern = "[^\\u4e00-\\u9fa5]+";
				Pattern pat = Pattern.compile(pattern);
				Matcher matcher = pat.matcher(password);
				if (!matcher.matches()) {
					throw new RuntimeException("不能为汉字");
				} else {
					userService.insertSelective(name,password,type);
					resultMessage.setSuccess(true);
					resultMessage.setMessage("添加成功！");
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/modificationpas", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "修改密码", notes = "")
	private ResultMessage modificationpas(@RequestParam String userName,
			@RequestParam String newpassword) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			String pattern = "[^\\u4e00-\\u9fa5]+";
			Pattern pat = Pattern.compile(pattern);
			Matcher matcher = pat.matcher(newpassword);
			if (!matcher.matches()) {
				throw new RuntimeException("不能为汉字");
			}
			userService.updateByuserNmaemodifipas(userName, "", newpassword);
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/userlistpagelist", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户列表", notes = "分页按名称搜索，按类型分类")
	private resultlist userlistpagelist(@RequestParam Integer page, @RequestParam Integer limit,
			@RequestParam(required = false) String userName, @RequestParam(required = false) Integer type) {
		resultlist resultMessage = new resultlist();
		try {
			if (page == null || page <= 0 || limit == null || limit <= 0) {
				resultMessage.setCode(1);
				resultMessage.setMsg("参数错误");
			} else {
				PageHelper.startPage(page, limit);
				System.out.println(userName);
				List<user> list = userService.userlistpagelist(userName, type);

				PageInfo<user> pageInfo = new PageInfo<>(list);
				resultMessage.setData(pageInfo.getList());
				resultMessage.setCount(pageInfo.getTotal());
				resultMessage.setCode(0);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/userlogin", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户登陆", notes = "")
	private ResultMessage userlogin(String userName,String password,HttpSession session, HttpServletRequest request) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(userName) || StringUtil.isEmpty(password)
					/*|| StringUtil.isEmpty(verify)*/) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数为空");
			}/* else if (!verify.equals(session.getAttribute("verify"))) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("验证码不正确");
			}*/ else {
				user user=new user();
				String ipstring = assist.getIpAdrress(request);
				user.setIpaddress(ipstring);
				user.setName(userName);
				user.setPassword(password);
				user=userService.userloginservice(user);
				user.setPassword(null);
				session.setAttribute("user", user);
				resultMessage.setData(user);
				resultMessage.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/userdelete", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "删除用户")
	private ResultMessage userdelete(String userid) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(userid)) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数为空");
			} else {
				userService.deleteuser(userid);
				resultMessage.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/verifyimg", method = RequestMethod.GET)
	@ApiOperation(value = "验证码", notes = "")
	private void verifyimg(HttpServletResponse response,HttpSession session) {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "No-cache");
		response.setContentType("image/jpeg");
		ServletOutputStream sos = null;
		try {
			sos = response.getOutputStream();
			Verification verification=new Verification();
			session.setAttribute("verify", verification.createverification(sos));
			sos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
